﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.IO;

namespace Providers
{
    /// <summary>
    /// Load the provider plugins 
    /// </summary>
    internal class PluginLoader
    {
        /// <summary>
        /// List of available SuperQuery server plug ins
        /// </summary>
        [ImportMany(AllowRecomposition = true)]
        public IEnumerable<SQPlugin.ISQPlugin> ServerPlugins
        {
            get;
            set;
        }

        /// <summary>
        /// Load server providers from the plug ins folder into the ServerPlugins property
        /// </summary>
        /// <param name="path">The location to load plug ins from</param>
        /// <returns>A collection of server provider plug ins</returns>
        public IEnumerable<SQPlugin.ISQPlugin> LoadServerPlugins(string path)
        {
            if (Directory.Exists(path))
            {
                DirectoryCatalog pluginCatalog = new DirectoryCatalog(path);
                CompositionContainer container = new CompositionContainer(pluginCatalog);
                container.GetExports<SQPlugin.ISQPlugin>();
                container.ComposeParts(this);
            }

            return ServerPlugins;
        }
    }
}
